package com.wcity.manage.web.property;

import com.wcity.manage.common.bean.RetBean;
import com.wcity.manage.common.context.Constants;
import com.wcity.manage.common.enums.Code;
import com.wcity.manage.common.exception.CustomException;
import com.wcity.manage.common.utils.StringUtil;
import com.wcity.manage.dao.dal.model.PageModel;
import com.wcity.manage.dao.model.Fix;
import com.wcity.manage.service.property.PropertyFixService;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * 前端物业报修模块
 * Created by zm on 2016/12/17.
 */
@Controller
@RequestMapping("v1")
public class IpropertyFixAct {

    @Resource private PropertyFixService propertyFixService;

    /**
     * 前端报修添加接口
     * @param blockName
     * @param name
     * @param addr
     * @param phone
     * @param areeType
     * @param images
     * @param content
     * @param blockId
     * @return
     */
    @RequestMapping ("fix_add")
    @ResponseBody
    public RetBean fixAdd (String blockName , String name , String addr ,
                           String phone , String areeType , String images , String content , String blockId ,String regId) {

        System.out.println(!StringUtil.isBlank( blockName ) +";"+!StringUtil.isBlank( name ) +";"+!StringUtil.isBlank( addr ) +";"+!StringUtil.isBlank( blockName ) +";"+StringUtil.isNumeric( phone , 11 ) +";"+!StringUtil.isBlank( areeType ) +";"+!StringUtil.isBlank( images ) +";"+!StringUtil.isBlank( content ) +";"+!StringUtil.isBlank( blockId ) +";"+!StringUtil.isBlank( regId ) );
        if( StringUtil.isBlank( blockName ) && StringUtil.isBlank( name )
            && StringUtil.isBlank( addr ) && !StringUtil.isNumeric( phone , 11 )
            && StringUtil.isBlank( areeType ) && StringUtil.isBlank( content )
            && StringUtil.isBlank( blockId ) && StringUtil.isBlank( regId ))
            throw new CustomException( Constants.PARAMETER_ERROR, Code.ERROR );

        String errorMsg = propertyFixService.fixAdd (blockName,name,addr,phone,areeType,images,content,blockId,regId);

        if(StringUtil.isBlank(errorMsg)) throw new CustomException( errorMsg, Code.ERROR );

        if(!errorMsg.equals("提交成功")) return new RetBean( Code.ERROR, errorMsg );

        return new RetBean( Code.SUCCESS, errorMsg );
    }

    /**
     * 获取我的报修信息列表
     * @param pageModel
     * @param regId
     * @return
     */
    @RequestMapping("I_fix_list")
    @ResponseBody
    public Map<String,Object> IfixList(PageModel pageModel, String regId ) {

        int STATUS = 1;//1为未删除；0为删除

        if(!StringUtil.isBlank( regId )) new CustomException( Constants.PARAMETER_ERROR, Code.ERROR );
        Fix fix = new Fix();
        fix.setRegId(Integer.parseInt(regId));
        fix.setRemark(STATUS);
        pageModel.setParams(fix);
        Map<String,Object> errorMeg = propertyFixService.getIFixList (pageModel);
        return errorMeg;
    }

    /**
     * 获取特定报修信息
     * @param id
     * @return
     */
    @RequestMapping("I_fix_info")
    @ResponseBody
    public Map<String,Object> fixInfo(String id) {
        if(!StringUtil.isBlank( id )) new CustomException( Constants.PARAMETER_ERROR, Code.ERROR );
        Map<String,Object> map = new HashMap<>();
        try {
            Fix fixInfo = propertyFixService.getFixInfo(id);
            map.put("id",fixInfo.getId());
            map.put("blockName",fixInfo.getBlockName());
            map.put("name",fixInfo.getName());
            map.put("addr",fixInfo.getAddr());
            map.put("phone",fixInfo.getPhone());
            map.put("content",fixInfo.getContent());
            map.put("status",fixInfo.getStatus());
            map.put("worker",fixInfo.getWorker());
            map.put("workerTel",fixInfo.getWorkerTel());
            map.put("areeType",fixInfo.getAreeType());
            map.put("createTime",fixInfo.getCreateTime());
            map.put("userId",fixInfo.getUserId());
            map.put("regId",fixInfo.getRegId());
            map.put("retCode",Code.SUCCESS.getCodeNum());
            map.put("retDesc","请求成功");
        }catch (Exception e){
            e.printStackTrace();
            map.put("err","服务器异常");
        }
        System.out.println(map);
        return map;
    }

    /**
     * 删除记录
     * @param fixId
     * @return
     */
    @RequestMapping("i_fix_dele")
    @ResponseBody
    public Map<String,Object> IFixDele(String fixId) {
        int STATUS = 0;//1为未删除；0为删除
        if(!StringUtil.isBlank( fixId )) new CustomException( Constants.PARAMETER_ERROR, Code.ERROR );
        Map<String,Object> map = propertyFixService.delect( fixId , STATUS );
        return map;
    }
}
